<?php

namespace app\admin\controller;

use think\Controller;
use think\Request;
use app\common\model\User as Us;

class User extends Controller
{
    //用户列表
    public function index()
    {
        $u = new Us();
        if ($this->request->isPost()) {
            $type = $this->request->param('type');
            $id = $this->request->param('id');
            if ($type == 'del') {
                if ($u->del($id)) {
                    return json_x(0, '删除成功');
                } else {
                    return json_x(-1, '删除失败');
                }
            }
            if ($type == 'delall') {
                if (Us::where('Id', '>', 0)->delete()) {
                    return json_x(0, '成功');
                } else {
                    return json_x();
                }
            }
        } else {
            if ($this->request->param('type') == 'get') {
                //获取分组信息
                $page = $this->request->param("page") ? $this->request->param("page") : 1;
                $page = intval($page);
                $limit = $this->request->param("limit") ? $this->request->param("limit") : 1;
                $limit = intval($limit);
                $start = $limit * ($page - 1);
                $key = $this->request->param('keyword');
                $user_list = $u->getuserlist($start, $limit, $key);
                return $user_list;
            }
            return $this->fetch('admin/main/user');
        }
    }

    //编辑用户
    public function edit_user()
    {
        if ($this->request->isPost()) {
            $u = new Us();
            $data = $this->request->param();
            if ($u->up_user($data, $data['id'])) {
                return json_x(0, '成功');
            } else {
                return json_x(-1, '失败');
            }
        } else {
            $info = Us::get($this->request->param('id'));
            return $this->fetch('admin/main/edit_user', ['info' => $info]);
        }
    }
    //后台开通vip
    public function open_vip()
    {
        if ($this->request->isPost()) {

            $data = $this->request->param();
            $u = new Us();
            if ($u->op_vip($data,1)) {
                return json_x(0, '成功');
            }
            return json_x(-1, '失败');
        } else {
            return json(['code' => 400]);
        }
    }

    /**
     * 充值积分
     * @return void
     */
    public function pay()
    {
        if ($this->request->isPost()) {
            $data = $this->request->param();
            $u = new Us();
            if ($u->admin_pay($data)) {
                return json_x(0, '成功');
            }
            return json_x(-1, '失败');
        } else {
            return json(['code' => 400]);
        }
    }
}
